package com.yaozheng.vocationaltraining.widget;

import net.sf.json.JSONArray;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.entity.view.MemoryWealthStatisticsEntity;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 学习记录统计view
 * 
 * @author Administrator
 * 
 */
public class MemoryWealthStatisticsView extends View {
	/**
	 * 圆画笔对象
	 */
	private Paint paint;
	/**
	 * 学习记录边框颜色
	 */
	private int memoryWealthStatisticsBorderColor;

	/**
	 * 学习过颜色
	 */
	private int memoryWealthStatisticsLearnedColor;

	/**
	 * 默认背景颜色
	 */
	private int memoryWealthStatisticsDefaultColor;
	/**
	 * 数据
	 */
	private MemoryWealthStatisticsEntity data[];
	/**
	 * 圆半径
	 */
	private int cirleRadius;

	private Paint textPaint;

	// private int

	private int height = 60;
	/**
	 * 当前view高度
	 */
	private int currViewHeight;

	public MemoryWealthStatisticsView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView();
	}

	public MemoryWealthStatisticsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();

	}

	public MemoryWealthStatisticsView(Context context) {
		super(context);
		initView();

		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (data != null) {

			int contentWidth = getWidth()
					- (((getWidth()) / (data.length - 1)) / 2) * 2;
			int width = (contentWidth / (data.length - 1));
			int x = ((contentWidth / data.length)) / 2;
			int y = cirleRadius;
			paint.setAntiAlias(true);
			/**
			 * 第一行
			 */
			/**
			 * 前面线
			 */
			paint.setColor(memoryWealthStatisticsBorderColor);// 设置灰色
			canvas.drawLine(0, y, x, y, paint);

			/**
			 * 前面线
			 */

			paint.setColor(memoryWealthStatisticsBorderColor);// 设置灰色
			canvas.drawLine(0, height, x, height, paint);
			/**
			 * 画正方形
			 */
			for (int i = 0; i < data.length; i++) {

				if (i == (data.length - 1)) {
					/**
					 * 最后面
					 */
					/**
					 * 前面线
					 */
					paint.setColor(memoryWealthStatisticsBorderColor);// 设置灰色
					canvas.drawLine(x, y, x + ((contentWidth / data.length))
							/ 2, y, paint);

					/**
					 * 前面线
					 */

					paint.setColor(memoryWealthStatisticsBorderColor);// 设置灰色
					canvas.drawLine(x, height, x
							+ ((contentWidth / data.length)) / 2, height, paint);
				} else {
					paint.setColor(memoryWealthStatisticsBorderColor);// 设置灰色
					paint.setStyle(Paint.Style.STROKE);// 设置填满
					/**
					 * 画正方型
					 */
					canvas.drawRect(x, y, x + width, height, paint);

				}
				x += width;

			}
			/**
			 * 画小圆
			 */
			x = ((contentWidth / data.length)) / 2;
			y = cirleRadius;
			for (int i = 0; i < data.length; i++) {
				/**
				 * 头部圆
				 */
				MemoryWealthStatisticsEntity memoryWealthStatisticsEntity = i == -1 ? null
						: data[i];
				if (memoryWealthStatisticsEntity == null
						|| memoryWealthStatisticsEntity.getStatus() == 0
						|| memoryWealthStatisticsEntity.getStatus() == 1) {
					// //直线
					paint.setColor(memoryWealthStatisticsLearnedColor);
				} else {
					paint.setColor(memoryWealthStatisticsDefaultColor);
				}

				paint.setStyle(Paint.Style.FILL_AND_STROKE);// 设置填满
				// canvas.drawCircle(x + (i == -1 ? 0 : width), cirleRadius,
				// cirleRadius, paint);
				//
				canvas.drawCircle(x, cirleRadius, cirleRadius, paint);
				// 底部圆 白色
				paint.setColor(Color.WHITE);
				/**
				 * 底部圆
				 */
				// canvas.drawCircle(x + (i == -1 ? 0 : width), height,
				// cirleRadius, paint);
				canvas.drawCircle(x, height, cirleRadius, paint);

				if (memoryWealthStatisticsEntity != null
						&& !"".equals(memoryWealthStatisticsEntity.getKeyName())) {

					Rect rect = new Rect();
					this.textPaint.getTextBounds(
							memoryWealthStatisticsEntity.getKeyName(), 0,
							memoryWealthStatisticsEntity.getKeyName().length(),
							rect);
					int textX = x - rect.centerX();// 让现实的字体处于中心位置;;
					canvas.drawText(memoryWealthStatisticsEntity.getKeyName(),
							textX, height + rect.height() + 5, textPaint);
				}
				if (i != -1) {
					x += width;
				}

			}
			/**
			 * 画曲线
			 */
			x = ((contentWidth / data.length)) / 2;
			y = cirleRadius;
			for (int i = 0; i < data.length - 1; i++) {
				MemoryWealthStatisticsEntity memoryWealthStatisticsEntity = data[i];
				paint.setStyle(Paint.Style.STROKE);// 设置填满
				paint.setColor(memoryWealthStatisticsLearnedColor);//
				switch (memoryWealthStatisticsEntity.getStatus()) {
				case 0:
					// //上直线
					canvas.drawLine(x, y, x + width, y, paint);
					break;
				case 1:
					// 下斜线
					canvas.drawLine(x, y, x + width, height, paint);
					break;
				case 2:
					// 上斜线
					canvas.drawLine(x, height, x + width, 0, paint);
					break;
				case 4:
					// 下直线
					canvas.drawLine(x, height, x + width, height, paint);
					break;
						
				default:
					break;
				}

				x += width;
			}

		}

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// int height = View.MeasureSpec.getSize(heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		setMeasuredDimension(width, currViewHeight); // 这里面是原始的大小，需要重新计算可以修改本行
	}

	/**
	 * 设置数据
	 * 
	 * @param dataValue
	 */
	public void setDataValue(JSONArray dataValue) {
		if (dataValue != null && TypeUtils.getJsonArraySize(dataValue) > 0) {
			for (int i = 0; i < data.length; i++) {
				boolean itemData = dataValue.optBoolean(i, false);
				MemoryWealthStatisticsEntity memoryWealthStatisticsEntity = data[i];
				if (itemData) {
					// if (i == 0) {
					// memoryWealthStatisticsEntity.setStatus(0);
					// } else {
					boolean previouSitemData = dataValue.optBoolean(i - 1,
							false);
					boolean lastSitemData = dataValue.optBoolean(i + 1, false);

					if (previouSitemData) {
						if (itemData) {
							// 第一个为true 当前为true
							if (lastSitemData) {
								// 第一个为true 当前为true 下一个 true
								memoryWealthStatisticsEntity.setStatus(4);
							} else {
								// 第一个为true 当前为true 下一个 false
								memoryWealthStatisticsEntity.setStatus(2);
							}

						} else {

							// 第一个为true 当前为false
							if (lastSitemData) {
								// 第一个为true 当前为false 下一个 true
								memoryWealthStatisticsEntity.setStatus(2);
							} else {
								// 第一个为true 当前为false 下一个 false
								memoryWealthStatisticsEntity.setStatus(0);
							}
						}
					} else {
						if (itemData) {
							// 第一个为false 当前为true
							if (lastSitemData) {
								// 第一个为false 当前为true 下一个 true
								memoryWealthStatisticsEntity.setStatus(1);
							} else {
								// 第一个为false 当前为true 下一个 false
								memoryWealthStatisticsEntity.setStatus(2);
							}
						} else {
							// 第一个为false 当前为false
							if (lastSitemData) {
								// 第一个为false 当前为false 下一个 true
								memoryWealthStatisticsEntity.setStatus(1);
							} else {
								// 第一个为false 当前为false 下一个 false
								memoryWealthStatisticsEntity.setStatus(0);
							}
						}
						// }
					}
				}
			}
			invalidate();
		}
	}

	/**
	 * 初始化View
	 */
	public void initView() {
		paint = new Paint();
		textPaint = new Paint();
		memoryWealthStatisticsDefaultColor = Color.BLACK;
		memoryWealthStatisticsLearnedColor = Color.RED;
		// memoryWealthStatisticsBorderColor = Color.parseColor("#b7b7b7");
		memoryWealthStatisticsBorderColor = getResources().getColor(
				R.color.memory_wealth_statistics_border_color);
		cirleRadius = 2;
		data = new MemoryWealthStatisticsEntity[] {
				new MemoryWealthStatisticsEntity("", 0),
				new MemoryWealthStatisticsEntity("20分钟", 0),
				new MemoryWealthStatisticsEntity("1小时", 0),
				new MemoryWealthStatisticsEntity("9小时", 0),
				new MemoryWealthStatisticsEntity("1天", 0),
				new MemoryWealthStatisticsEntity("2天", 0),
				new MemoryWealthStatisticsEntity("6天", 0),
				new MemoryWealthStatisticsEntity("一月", 0) };

		this.textPaint = new Paint();
		this.textPaint.setAntiAlias(true);// 设置抗锯齿;;;;
		this.textPaint.setColor(memoryWealthStatisticsDefaultColor);
		float mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
				10, getResources().getDisplayMetrics());
		this.textPaint.setTextSize(mTextSize);

		height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				45, getResources().getDisplayMetrics());
		currViewHeight = height;
		if (data != null) {
			MemoryWealthStatisticsEntity memoryWealthStatisticsEntity = data[1];
			Rect rect = new Rect();
			this.textPaint.getTextBounds(
					memoryWealthStatisticsEntity.getKeyName(), 0,
					memoryWealthStatisticsEntity.getKeyName().length(), rect);
			currViewHeight += rect.height() + 10;
		}

	}
}
